package com.chtw.vuedemo.service.impl;

import com.chtw.vuedemo.bean.Brands;
import com.chtw.vuedemo.bean.BrandsExample;
import com.chtw.vuedemo.mapper.BrandsMapper;
import com.chtw.vuedemo.service.inf.BrandsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author chtw
 * @date 2020-09-27 23:18
 */
@Service
public class BrandsServiceImpl implements BrandsService {

    @Autowired
    private BrandsMapper brandsMapper;

    final byte IS_DELETE = 1;
    final byte NOT_IS_DELETE = 0;

    @Override
    public List<Brands> getAllBrands() {
        BrandsExample example = new BrandsExample();
        BrandsExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(NOT_IS_DELETE);
        return brandsMapper.selectByExample(example);
    }

    @Override
    public Brands getById(int id) {
        return brandsMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean insert(Brands brands) {
        brands.setIsDelete(NOT_IS_DELETE);
        if (brands.getCreateTime() == null){
            brands.setCreateTime(new Date());
        }
        int inserted = brandsMapper.insert(brands);
        return inserted > 0;
    }

    @Override
    public boolean delete(int id){
        // 真删除
        //int deleted = brandsMapper.deleteByPrimaryKey(id);

        // 假删除
        Brands brands = new Brands();
        brands.setIsDelete(IS_DELETE);
        brands.setId(id);
        int deleted = brandsMapper.updateByPrimaryKeySelective(brands);
        return deleted > 0;
    }

    @Override
    public List<Brands> search(Brands brands) {
        BrandsExample example = new BrandsExample();
        BrandsExample.Criteria criteria = example.createCriteria();
        criteria.andNameLike("%" + brands.getName() + "%");
        criteria.andIsDeleteEqualTo(NOT_IS_DELETE);
        return brandsMapper.selectByExample(example);
    }
}
